package com.jams.goodox.activity.serve.visitor;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.hangzhou.jin.customview.LoadView;
import com.jams.goodox.BaseFragment;
import com.jams.goodox.R;
import com.jams.goodox.constant.Constant;
import com.jams.goodox.entity.VisitorEwmBean;
import com.jams.goodox.entity.VisitorHistoryBean;
import com.jams.goodox.net.RetrofitCallbackListener;
import com.jams.goodox.net.services.ServiceService;
import com.jams.goodox.utils.ToastUtil;
import com.jams.goodox.widget.LoadMoreListView;
import com.jams.library.commonAdapter.ListCommonAdapter;
import com.jams.library.commonAdapter.ListCommonViewHolder;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import retrofit2.Call;

/**
 * 访客通行记录界面
 * Created by jams_zhen on 2017/5/11.
 */

public class VisitorHistoryFragment extends BaseFragment implements LoadMoreListView.OnLoadMoreListener, SwipeRefreshLayout.OnRefreshListener,AdapterView.OnItemClickListener{
    @Bind(R.id.lst)
    LoadMoreListView lst;
    @Bind(R.id.refresh_layout)
    SwipeRefreshLayout refreshLayout;
    @Bind(R.id.load_view)
    LoadView loadView;

    private ListCommonAdapter listCommonAdapter;

    private List<VisitorEwmBean.DataBean> resultListBeanList;
    private int curPage = 1;

    private boolean isArrive;

    public static VisitorHistoryFragment getInstance(boolean isArrive){
        VisitorHistoryFragment visitorHistoryFragment=new VisitorHistoryFragment();
        Bundle bundle=new Bundle();
        bundle.putBoolean("isArrive",isArrive);
        visitorHistoryFragment.setArguments(bundle);
        return visitorHistoryFragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.visitor_history_fragment, container, false);
        ButterKnife.bind(this, view);
        setDate();
        refreshLayout.setRefreshing(true);
        getDate(1);
        return view;
    }

    private void getDate(int curUsePage) {
        Map<String, String> map = Constant.getPagerParams(curUsePage);
        map.put("status", isArrive ? "1" : "0");
        enqueue(createCallService(ServiceService.class).queryExpressHistory(map), new RetrofitCallbackListener<VisitorHistoryBean>() {
            @Override
            public void onSuccess(Call<VisitorHistoryBean> call, VisitorHistoryBean visitorHistoryBean) {
                loadView.loadComplete();
                refreshLayout.setRefreshing(false);//刷新菊花关闭
                lst.onLoadMoreComplete();

                VisitorHistoryBean.DataBean dataBean=visitorHistoryBean.getData();
                //此处若发现异常数据，结束处理
                if (dataBean == null || dataBean.getPageInfo() == null || dataBean.getResultList() == null) {
                    if (resultListBeanList.size() == 0) loadView.loadCompleteNoData(R.mipmap.holder_no_history,"暂无记录");
                    return;
                }
                curPage = dataBean.getPageInfo().getCurPage();
                lst.setCanLoadMore(dataBean.getPageInfo().isHasMore());
                if(curPage==1) resultListBeanList.clear();//当前页为第一页，清空数据源
                resultListBeanList.addAll(visitorHistoryBean.getData().getResultList());
                listCommonAdapter.notifyDataSetChanged();
                if (resultListBeanList.size() == 0) loadView.loadCompleteNoData(R.mipmap.holder_no_history,"暂无记录");
            }

            @Override
            public void onError(String msg) {
                refreshLayout.setRefreshing(false);//刷新菊花关闭
                lst.onLoadMoreComplete();
                loadView.loadComplete();
                ToastUtil.show(msg);
                if (resultListBeanList.size() == 0)  loadView.loadCompleteNoData(R.mipmap.holder_no_history,"暂无记录");
            }
        });
    }

    private void setDate() {
        isArrive = getArguments().getBoolean("isArrive", true);
        lst.setOnLoadMoreListener(this);
        refreshLayout.setOnRefreshListener(this);
        resultListBeanList = new ArrayList<>();
        listCommonAdapter = new ListCommonAdapter<VisitorEwmBean.DataBean>(getActivity(), resultListBeanList, R.layout.item_visitor_history) {
            @Override
            public void convert(ListCommonViewHolder helper, final VisitorEwmBean.DataBean item, final int position) {
                TextView tx_name = helper.getView(R.id.tx_name);
                TextView tx_date = helper.getView(R.id.tx_date);
                ImageView imgEwm=helper.getView(R.id.img_ewm);

                if(isArrive||item.getIsReach().equalsIgnoreCase("0")){
                    tx_date.setText("再次邀约");
                    tx_date.setTextColor(ContextCompat.getColor(getContext(),R.color.font_color_gray));
                    imgEwm.setVisibility(View.GONE);
                }else {
                    tx_date.setText(item.getScheduleAt());
                    tx_date.setTextColor(ContextCompat.getColor(getContext(),R.color.font_color_black));
                    imgEwm.setVisibility(View.VISIBLE);
                }

                String sex="先生";
                if(item.getGender()!=1){
                    sex="女士";
                }
                tx_name.setText(item.getSurname() + sex);
            }
        };
        lst.setAdapter(listCommonAdapter);
        lst.setOnItemClickListener(this);
    }

    @Override
    public void onLoadMore() {
        getDate(curPage + 1);
    }

    @Override
    public void onRefresh() {
        getDate(1);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
        VisitorEwmBean.DataBean item=resultListBeanList.get(position);
        if(isArrive||item.getIsReach().equalsIgnoreCase("0")){
            Intent in = new Intent(getActivity(), VisitorAddActivity.class);
            in.putExtra("visitorEwmBean", item);
            startActivity(in);
            getActivity().finish();
        }else {
        }
    }
}
